/**
 * @author 雷明浩
 * @date 2025/5/29 16:50
 */
public class DiameterOfBinaryTree {
    // leetcode 二叉树的直径
    // https://leetcode.cn/problems/diameter-of-binary-tree/?envType=study-plan-v2&envId=top-100-liked
    private int ret;
    public int diameterOfBinaryTree(TreeNode root) {
        ret = 1;
        dfs(root);
        return ret-1;
    }

    private int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = dfs(root.left);
        int right = dfs(root.right);
        ret = Math.max(ret, left + right + 1);
        return Math.max(left, right) +1;
    }
}